<template>
  <el-dialog
    title="新增原材料属性"
    :visible.sync="dialogVisible"
    width="600px"
    customClass="mes_dialog"
    v-dialogDrag
    :close-on-click-modal="false">
    <div class="raw_prop_cont" :class="{more_list: rawPropList.length > 10}">
      <el-scrollbar>
        <div class="raw_item">
          <div class="raw_label"><span class="raw_label_required">*</span>物料编码</div>
          <div class="raw_input">
            <el-input v-model="materialNumber" maxLength="20" size="small" placeholder="请输入物料编码"></el-input>
          </div>
        </div>
        <div class="raw_item">
          <div class="raw_label"><span class="raw_label_required">*</span>物料名称</div>
          <div class="raw_input">
            <el-input v-model="materialName" maxLength="20" size="small" placeholder="请输入物料名称"></el-input>
          </div>
        </div>
        <div class="raw_item"
             :key="item.label"
             v-for="item in rawPropList">
          <div class="raw_label" v-html="item.label"></div>
          <div class="raw_input">
            <el-input v-model="item.val" maxLength="10" size="small" placeholder="请输入字段名"></el-input>
          </div>
        </div>
      </el-scrollbar>
    </div>
    <div class="raw_add_item">
      <span class="add_item_btn" @click="addItem"><i class="iconfont icon-btn_addnew"></i>添加10条</span>
    </div>
    <div slot="footer" class="mes_btn">
      <el-button type="primary" @click="verifyData">提交</el-button>
      <el-button @click="_close">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'AddProp',
  data() {
    return {
      dialogVisible: false,
      materialNumber: '', // 物料编码
      materialName: '', // 物料名称
      rawPropList: [], // 字段列表
    }
  },
  methods: {
    // 添加10条空字段
    addItem(num) {
      let sum = typeof num === 'number' ? num : 10
      for (let i = 0; i < sum; i++) {
        this.rawPropList.push({label: `字段${this.rawPropList.length + 1}`, val: ''})
      }
    },
    // 显示弹框
    openDialog() {
      this.init()
      this.dialogVisible = true
    },
    // 初始化
    init() {
      this.materialNumber = ''
      this.materialName = ''
      this.rawPropList = []
      this.addItem(8)
    },
    // 数据验证
    verifyData() {
      let template = this.rawPropList.filter(item => item.val).reduce((pre, cur) => [...pre, cur.val], []).join(',')
      if (!this.materialNumber) {
        this.$message({
          type: 'error',
          message: '请填写物料编码',
          duration: 1500,
          center: true
        })
      } else if (!this.materialName) {
        this.$message({
          type: 'error',
          message: '请填写物料名称',
          duration: 1500,
          center: true
        })
      } else if (!template.length) {
        this.$message({
          type: 'error',
          message: '请至少填写一个字段',
          duration: 1500,
          center: true
        })
      } else {
        this.submit(template)
      }
    },
    // 保存
    submit(template) {
      let postData = {
        number: this.materialNumber,
        name: this.materialName,
        template: template
      }
      this.confirmOpen('原材料属性新增后不能修改，确定提交吗？', '保存提示').then(() => {
        this.$store.dispatch('saveData', {
          url: 'a/rawmaterialtemplate/rawMaterialTemplate/saveTemplate',
          method: 'post',
          data: postData,
          noCommit: true
        })
      }).then(() => {
        this.$emit('saveSuccess')
        this.dialogVisible = false
        this.$message({
          type: 'success',
          message: '保存成功',
          duration: 1500,
          center: true
        })
      })
    },
    // 关闭弹框
    _close() {
      this.dialogVisible = false
    }
  }
}
</script>

<style scoped lang="less">
  .raw_prop_cont{
    border: 1px solid #dbe1e8;
    height: 409px;
    &.more_list{
      height: 430px;
    }
    .el-scrollbar {
      height: 100%;
      .raw_label{
        flex: 0 0 120px;
        text-align: right;
      }
    }
  }
</style>
